Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

154
Vistas
Why am I getting an "undefined" result from my MongoDBQuery in Node?

I've already stablished a DB with it's schema like this:
enter image description here

When I use my search function in Nodejs it works perfectly and returns my info like this:\

enter image description here

So, why is is than when I try to fectch my user from mongodb and get the parameter "examenes":
enter image description here

I get this undefined return?:
enter image description here

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

I think you should use the await keyword

try{
   var user = await model.findOne({ _id: req.params.id});
} catch (e) {
   // Handle Errors
};

Or

model.findOne({ _id: req.params.id}).then(result => {
  var user = result;
}).catch(e=> {
// Handle Errors
});
about 4 years ago · Juan Pablo Isaza Denunciar

0

Have you added mergeParams in your router? This options adds params object to the request const router = express.Router({ mergeParams: true });

then /predict/:id results in req.params.id

By default this option is disabled in express

about 4 years ago · Juan Pablo Isaza Denunciar

0

The problem is you are not using await so in the line console.log(user.examenes) the variable user is not the data returned by mongo; is another object without attribute examenes. And trying to access the value is undefined.

So you can use:

let user = await (usuario.findOne({_id: req.params.id}))
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda